package com.atguigu.order.listener;

import com.atguigu.order.entity.OrderEntity;
import com.atguigu.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * description: OrderCloseListener
 * date: 2022/12/30 15:09
 * author: MR.孙
 */
@Slf4j
@Component
@RabbitListener(queues = {"order.release.order.queue"})
public class OrderCloseListener {


    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity order, Channel channel, Message message) throws IOException {
        log.info("收到过期的订单消息，准备关闭订单" + order.getOrderSn());
        try {
            orderService.closeOrder(order);
            //手动ACK
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //执行失败重新发送到消息队列, 方便其他人消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
